_package("alz.gesture");

_import("alz.gesture.Touch");

/**
 * 触摸手势
 */
_class("Touch", Gesture, function(){
	this.handles = ["touchstart", "touchmove", "touchend", "touchdown"];
	this.touchDownInterval = 500;
	this._init = function(config){
		_super._init.apply(this, arguments);
	};
	this.onTouchStart = function(ev, touch){
		this.startX = this.previousX = touch.pageX;
		this.startY = this.previousY = touch.pageY;
		this.startTime = this.previousTime = ev.timeStamp;
		this.fire("touchstart", ev);
		this.lastEvent = ev;
		if(this.listeners && this.listeners.touchdown){
			this.touchDownIntervalId = window.setInterval(runtime.createDelegate(this, "touchDownHandler"), this.touchDownInterval);
		}
	};
	this.onTouchMove = function(ev, touch){
		this.fire("touchmove", ev, this.getInfo(touch));
		this.lastEvent = ev;
	};
	this.onTouchEnd = function(ev){
		this.fire("touchend", ev, this.lastInfo);
		window.clearInterval(this.touchDownIntervalId);
	};
	this.touchDownHandler = function(){
		this.fire("touchdown", this.lastEvent, this.lastInfo);
	};
	this.getInfo = function(touch){
		var time = Date.now(),
			deltaX = touch.pageX - this.startX,
			deltaY = touch.pageY - this.startY,
			info = {
				"startX"   : this.startX,
				"startY"   : this.startY,
				"previousX": this.previousX,
				"previousY": this.previousY,
				"pageX"    : touch.pageX,
				"pageY"    : touch.pageY,
				"deltaX"   : deltaX,
				"deltaY"   : deltaY,
				"absDeltaX": Math.abs(deltaX),
				"absDeltaY": Math.abs(deltaY),
				"previousDeltaX"   : touch.pageX - this.previousX,
				"previousDeltaY"   : touch.pageY - this.previousY,
				"time"             : time,
				"startTime"        : this.startTime,
				"previousTime"     : this.previousTime,
				"deltaTime"        : time - this.startTime,
				"previousDeltaTime": time - this.previousTime
			};
		this.previousTime = info.time;
		this.previousX = info.pageX;
		this.previousY = info.pageY;
		this.lastInfo = info;
		return info;
	};
});